﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using nelech.Models.DBModel;
using System.IO;
using nelech.Models.CalendarModel;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;
using System.Linq;
using System.Net;
using nelech.Controllers.Utils;
using System.Globalization;
using System.Text;


namespace nelech.Controllers
{
    public class EventController : Controller
    {
        private const string baseCalendarAddress = "http://www.google.com/calendar/feeds/nelech.il@gmail.com/private/full";
        CalendarService calendarService = new CalendarService("nelech");

        public EventController()
        {
            calendarService.setUserCredentials("nelech.il@gmail.com", "naftali_25");
        }

        public ActionResult Month()
        {
            CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL");
            var hc = new System.Globalization.HebrewCalendar();
            var now = DateTime.Now;
            var day = hc.GetDayOfMonth(now);
            var month = hc.GetMonth(now);
            var year = hc.GetYear(now);

            var daysInMonth = hc.GetDaysInMonth(year, month);
            var startDay = (now - new TimeSpan(day - 1, 0, 0, 0));
            var startDate = new DateTime(startDay.Year, startDay.Month, startDay.Day, 0, 0, 0);
            var finishDate = startDate + new TimeSpan(daysInMonth - 1, 0, 0, 0);

            var query = new EventQuery(baseCalendarAddress);
            query.StartTime = startDay;
            query.EndTime = finishDate;
            var feed = calendarService.Query(query);
            return View((from e in feed.Entries select e as EventEntry).OrderBy(p => p.Times[0].StartTime));

        }

        public ActionResult Day()
        {
            var date = DateTime.Now;
            var query = new EventQuery(baseCalendarAddress);
            query.StartTime = date;
            query.EndTime = date + new TimeSpan(1, 0, 0, 0, 0); ;
            var feed = calendarService.Query(query);
            return View((from e in feed.Entries select e as EventEntry).OrderBy(p => p.Times[0].StartTime));
        }

        public ActionResult ShooseDate()
        {
            return View();
        }

        public ActionResult ShowDetails(string id)
        {
            id = id.Split(new char[] { '@' })[0];
            var singleQuery = new FeedQuery();
            singleQuery.Uri = new Uri("http://www.google.com/calendar/feeds/nelech.il@gmail.com/private/full/" + id);
            var newFeed = calendarService.Query(singleQuery);
            var retrievedEntry = newFeed.Entries[0];
            var eventEntry = retrievedEntry as EventEntry;
            var eventTime = eventEntry.Times[0];

            var client = new WebClient();
            var data = (eventEntry.WebContentLink != null && eventEntry.WebContentLink.WebContent != null) ? client.DownloadData(eventEntry.WebContentLink.WebContent.Url) : null;
            var result = (eventEntry.WebContentLink != null && eventEntry.WebContentLink.WebContent != null) ? Encoding.UTF8.GetString(data) : null;
            var @event = new Event()
            {
                id = id,
                Day = eventTime.StartTime.Day,
                Details = eventEntry.Content.Content,
                EndHour = eventTime.EndTime.Hour,
                EndMinute = eventTime.EndTime.Minute,
                MceDoc = result,
                Month = eventTime.StartTime.Month,
                Photo = eventEntry.WebContentLink != null ?
                    (eventEntry.WebContentLink.HRef != null ? eventEntry.WebContentLink.HRef.Content : string.Empty) :
                    string.Empty,
                StartHour = eventTime.StartTime.Hour,
                StartMinute = eventTime.StartTime.Minute,
                Title = eventEntry.Title.Text,
                Year = eventTime.StartTime.Year
            };

            return View(@event);
        }

    }
}
